package hu.test.atacama;

import android.os.HandlerThread;
import android.os.PowerManager;
import android.util.Log;

public class WakeThread extends HandlerThread {
	  private PowerManager.WakeLock lock=null;

	  WakeThread(PowerManager.WakeLock lock, String name) {
	    super(name);

	    this.lock=lock;
	  }

	  protected void onPreExecute() {
		  
	  }

	  protected void onPostExecute() {
	    if (lock.isHeld()) {
	      lock.release();
	    }

	    if (!lock.isHeld()) {
	      onUnlocked();
	    }
	  }

	  protected void onUnlocked() {
		  
	  }

	  @Override
	  protected void onLooperPrepared() {
	    try {
	      onPreExecute();
	    }
	    catch (RuntimeException e) {
	      Log.e("WakeThread", "Exception onLooperPrepared()", e);
	      onPostExecute();
	      throw(e);
	    }
	  }

	  @Override
	  public void run() {
	    try {
	      super.run();
	    }
	    finally {
	      onPostExecute();
	    }
	  }
	}